package com.csy.property.controller.community;

import cn.afterturn.easypoi.excel.entity.ExportParams;
import com.csy.commons.core.controller.BaseController;
import com.csy.commons.core.domain.BaseResponse;
import com.csy.commons.core.utils.ExcelUtils;
import com.csy.property.community.domain.AjCommunity;
import com.csy.property.community.domain.dto.AjCommunityDto;
import com.csy.property.community.domain.dto.AjCommunityExcelDto;
import com.csy.property.community.service.AjCommunityService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.stream.Collectors;


@Controller
@RequestMapping("/exportExcel")
public class ExportExcelController extends BaseController {
    @Resource
    private AjCommunityService communityService;

    /**
     *导出小区数据
     */
    @GetMapping("/exportCommunityExcel")
    public BaseResponse exportExcel(AjCommunity ajCommunity, HttpServletResponse response){
        startPage();
        List<AjCommunityDto> list = communityService.queryList(ajCommunity);

        //数据转换
        List<AjCommunityExcelDto> excelDtoList = list.stream().map(ajCommunityDto -> {
            AjCommunityExcelDto excelDto = new AjCommunityExcelDto();
            excelDto.setCommunityId(ajCommunityDto.getCommunityId());
            excelDto.setCommunityName(ajCommunityDto.getCommunityName());
            excelDto.setCommunityCode(ajCommunityDto.getCommunityCode());
            excelDto.setCommunityProvinceName(ajCommunityDto.getCommunityProvinceName());
            excelDto.setCommunityCityName(ajCommunityDto.getCommunityCityName());
            excelDto.setCommunityTownName(ajCommunityDto.getCommunityTownName());
            excelDto.setCreateTime(ajCommunityDto.getCreateTime());
            excelDto.setRemark(ajCommunityDto.getRemark());

            return excelDto;

        }).collect(Collectors.toList());

        ExcelUtils.exportExcel(excelDtoList, AjCommunityExcelDto.class,"小区信息.xls",response,
                new ExportParams("小区信息列表","小区信息"));

        return BaseResponse.success("导出小区信息到Excel成功！");
    }

}

















